package com.ruoyi.web.controller.api;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.QiNiuConfig;
import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.SendMsgType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.redis.RedisCacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @author zhimin.hu
 * @Date 2021/1/16
 */
@Api(value = "通用接口",tags = "公共接口")
@RestController
@RequestMapping("/api/common")
public class CommonApiController extends BaseApiController {

    @Autowired
    private RedisCacheUtil redisCache;
    @Autowired
    private QiNiuConfig qiNiuConfig;
    /**
     * 发送短信验证码
     * type  0 登陆
     * 1 注册
     * 2 修改密码
     * 3
     */
    @ApiOperation("发送短信验证码")
    @PostMapping("/getMsgCode")
    @ResponseBody
    public AjaxResult getMsgCode(String mobile,int type,String content)
    {
        String msgCode= RandomUtil.randomNumbers(6);
        if(StringUtils.isBlank(mobile)){
            return AjaxResult.error("手机号不能为空");
        }
        if(type>0 && StringUtils.isBlank(content)){
            return AjaxResult.error("发送内容不能为空");
        }
        msgCode="999999";
        SendMsgType sendMsgType=SendMsgType.getSendMsgTypeByCode(type);
         if(SendMsg(mobile,sendMsgType,msgCode)>0){
             redisCache.setCacheObject(RedisConstants.MESSAGE_CODE_PREFIX+sendMsgType.getCode()+"-"+mobile,msgCode,120, TimeUnit.MINUTES);
             return  AjaxResult.success();
         }else{
             return AjaxResult.error("发送失败请重试！");
         }

    }
    /**
     * 七牛上传图片请求 markdown
     */
    @ResponseBody
    @PostMapping("/qiNiu/markDownUpload")
    public JSONObject markDownUpload(HttpServletRequest request, HttpServletResponse response, @RequestParam(value ="editormd-image-file",required = false) MultipartFile file)  {
        JSONObject ajax=new JSONObject();
        ajax.put("success", 0);
        try {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");
            if (file != null && !file.isEmpty()) {
                // 上传并返回新文件名称
                String fileName = qiNiuConfig.uploadQiNiu(file);
                if (StringUtils.isNotBlank(fileName)) {
                    String url = qiNiuConfig.getHttpUrl() + fileName;
                    ajax.put("success", 1);
                    ajax.put("message", fileName);
                    ajax.put("url", url);
                    return ajax;
                } else {
                    ajax.put("message","上传失败，请重试！");
                    return ajax;
                }
            } else {
                ajax.put("message","请选择上传文件，请重试！");
                return ajax;
            }
        } catch (Exception e) {
            ajax.put("message",e.getMessage());
            return ajax;
        }
    }
    /**
     * 七牛上传图片请求
     */
    @ResponseBody
    @PostMapping("/qiNiu/upload")
    public AjaxResult qiniuUploadFile(@RequestParam("file") MultipartFile file)  {
        try {
            if (file != null && !file.isEmpty()) {
                // 上传并返回新文件名称
                String fileName = qiNiuConfig.uploadQiNiu(file);
                if (StringUtils.isNotBlank(fileName)) {
                    String url = qiNiuConfig.getHttpUrl() + fileName;
                    AjaxResult ajax = AjaxResult.success();
                    ajax.put("fileName", fileName);
                    ajax.put("url", url);
                    return ajax;
                } else {
                    return AjaxResult.error("上传失败，请重试！");
                }
            } else {
                return AjaxResult.error("请选择上传文件");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




    private int SendMsg(String mobile, SendMsgType type, String content){
        return  1;
    }

}
